package com.css.fxfzysx.modules.vaolEngineering.utils;

import com.css.fxfzysx.base.dict.service.DictService;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxMainTableEntity;
import com.css.fxfzysx.modules.lifeline.utils.ShpUtils;
import com.css.fxfzysx.modules.vaolEngineering.constants.BusinessObjectEnum;
import com.css.fxfzysx.modules.vaolEngineering.constants.TypeObjectConstants;
import com.css.fxfzysx.modules.vaolEngineering.repository.ValEngineeringRepository;
import com.css.fxfzysx.util.PlatformDateUtils;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.sun.istack.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MainTableUtils {

    @Autowired
    private ValEngineeringRepository valEngineeringRepository;

    @Autowired
    private ShpUtils shpUtils;

    @Autowired
    private DictService dictService;

    public synchronized String getCode(@NotNull String type, @NotNull String fortification, @NotNull Integer years) {
        String versionCode = "";
        String code = valEngineeringRepository.getCodeByType(type);
        if (PlatformObjectUtils.isNotEmpty(code)) {
            String[] split = code.split("-");
            versionCode = split[3];
            StringBuilder stringBuilder = new StringBuilder();
            String t = String.valueOf(Integer.parseInt(versionCode) + 1);
            for (int i = t.length(); i < 4; i++) {
                stringBuilder.append("0");
            }
            versionCode = stringBuilder.append(t).toString();
        } else {
            versionCode = "0001";
        }
        String title = BusinessObjectEnum.getByName(type);
        versionCode = title + "-" + getFs(fortification) + "-" + years + "-" + versionCode;
        return versionCode;
    }

    public static String getFs(String fs) {
        String result = "";
        switch (fs) {
            case "未设防":
                result = "W";
                break;
            case "其他":
                result = "Q";
                break;
            case "自设防":
                result = "Z";
                break;
            case "6度":
                result = "6";
                break;
            case "7度":
                result = "7";
                break;
            case "8度":
                result = "8";
                break;
            case "9度":
                result = "9";
                break;
            case "已设防":
                result = "Y";
                break;
            case "已有损坏":
                result = "S";
        }
        return result;
    }

    public static String getYear(Integer year) {
        if (year <= 1978) {
            return "1978年以前";
        }
        if (year > 1978 && year <= 1989) {
            return "1978-1989年";
        }
        if (year > 1989 && year <= 2001) {
            return "1989-2001年";
        }
        if (year > 2001) {
            return "2001年以后";
        }
        return "";
    }

    public void saveMainTable(SmxMainTableEntity entity) {
        entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        entity.setDelFlag("0");
        entity.setCreateUserName(shpUtils.getUserName(entity.getCreateUser()).getUserName());
        entity.setOrgName(shpUtils.getUserName(entity.getCreateUser()).getOrgName());
        if (PlatformObjectUtils.isEmpty(entity.getCalculationStatus())) {
            entity.setCalculationStatus(0);
        }
        entity.setType(TypeObjectConstants.getConstant(entity.getBusinessObject()));
        if (PlatformObjectUtils.isNotEmpty(entity.getType())) {
            if (entity.getType() == 5) {
                entity.setCode(getCode(entity.getBusinessObject(), getValueByKey("smxdlsfqk", entity.getFortification()), entity.getYear()));
            } else {
                entity.setCode(getCode(entity.getBusinessObject(), getValueByKey("smxsfqk", entity.getFortification()), entity.getYear()));
            }
        }
        valEngineeringRepository.saveMainTable(entity);
    }

    public void updateCalculationStatus(String id,String state6,String state7,String state8,String state9,String state10, Integer calculationStatus) {
        valEngineeringRepository.updateCalculationStatus(id,state6,state7,state8,state9,state10, calculationStatus);
    }

    public void updateModelBaseIds(String modelIds1, String modelIds2, String id) {
        valEngineeringRepository.updateModelBaseIds(modelIds1, modelIds2, id);
    }

    public String getValueByKey(String code, String key) {
        if (!PlatformObjectUtils.isEmpty(key)) {
            return dictService.getValueByDictCodeAndKey(code, key);
        }
        return null;
    }

    public String getKeyByValue(String code, String value) {
        if (!PlatformObjectUtils.isEmpty(value)) {
            String key = dictService.getKeyByDictCodeAndValue(code, value.trim());
            return key;
        }
        return null;
    }

    public void updateVersionCalculationStatus(String versionId, Integer status) {
        valEngineeringRepository.updateVersionCalculationStatus(versionId, status);
    }

    public void updateUpNetCalculationStatus(String tableId, int range) {
        valEngineeringRepository.updateUpNetCalculationStatus(tableId,range);
    }
}
